// order//live/myLiveList/index.js
const app = getApp();
const utils = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type: 0,//0商家看1用户看
    liveList: [],
    isCanApply: true,//可提交直播申请
    showRuler: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.type){
      this.setData({
        type: options.type
      })
    }
    this.getLiveList();
  },
  getLiveList: function () {
    var that = this;
    let type = that.data.type;
    let cardId = wx.getStorageSync('cardId');
    wx.request({
      url: app.globalData.host + '/api/Staff/GetInfo?id=' + cardId,
      success: function(res){
        if(res.data.Data.Uid){
          wx.request({
            url: app.globalData.host + '/api/weapp/GetLiveListByUser?uid=' + res.data.Data.Uid + '&storeId=' + app.globalData.storeId,
            success: function (res) {
              let liveList = res.data.Data.Items;
              let showList = [];
              if (liveList.length > 0) {
                //101 直播中102 未开始103 已结束104 禁播105 暂停中106 异常107 已过期
                for (let i = 0; i < liveList.length; i++) {
                  liveList[i].CoverImg = utils.formatAvater(liveList[i].CoverImg, 'https://static.711688.net.cn/upload/adv/');
                  liveList[i].ShareImg = utils.formatAvater(liveList[i].ShareImg, 'https://static.711688.net.cn/upload/adv/');
                  if(liveList[i].CheckStatus == 0){
                    liveList[i].status = '审核中'
                  }
                  else if (liveList[i].LiveStatus == 101) {
                    liveList[i].status = '直播中'
                  }
                  else if (liveList[i].LiveStatus == 102) {
                    liveList[i].status = '未开始'
                  }
                  else if (liveList[i].LiveStatus == 103) {
                    liveList[i].status = '已结束'
                  }
                  else if (liveList[i].LiveStatus == 104) {
                    liveList[i].status = '禁播'
                  }
                  else if (liveList[i].LiveStatus == 105) {
                    liveList[i].status = '暂停中'
                  }
                  else if (liveList[i].LiveStatus == 106) {
                    liveList[i].status = '异常'
                  }else {
                    liveList[i].status = '已过期'
                  }
                  if(liveList[i].CheckStatus == 2){
                    liveList[i].status = '未通过'
                  }
                  if(liveList[i].CheckStatus == 2 || liveList[i].CheckStatus == 0){//有未审核或审核未通过申请不可重新提交申请
                    that.data.isCanApply = false;
                  }
                  if(type==1){//用户角度看直播列表
                    if(liveList[i].CheckStatus == 1 && liveList[i].LiveStatus != 107){
                      showList.push(liveList[i]);
                    }
                  }else{
                    showList = liveList;
                  }
                }
              }
              that.setData({
                liveList: showList
              })
            },
          })
        }
      }
    })
  },
  toAddApply: function(){
    if(this.data.isCanApply){
      let lastid = 0;
      let liveList = this.data.liveList;
      if(liveList.length>0){
        lastid = liveList[0].Id
      }
      wx.navigateTo({
        url: '../apply/index?lastid=' + lastid,
      })
    }else{
      this.checkWrite('当前已有审核中或审核未通过的申请');
      return;
    }
  },
  toLookLive: function(e){
    let roomid = e.currentTarget.dataset.roomid;
    let id = e.currentTarget.dataset.id;
    let that = this;
    let cardId = wx.getStorageSync('cardId');
    let livePlayer = requirePlugin('live-player-plugin');
    livePlayer.getLiveStatus({ room_id: roomid })
        .then(res => {
        // 101: 直播中, 102: 未开始, 103: 已结束, 104: 禁播, 105: 暂停中, 106: 异常，107：已过期 
            const liveStatus = res.liveStatus
            that.changeLiveStatus(id, liveStatus);
        })
        .catch(err => {
        })
    wx.navigateTo({
      url: 'plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=' + roomid + '&custom_params=' + encodeURIComponent(JSON.stringify({
            cardId: cardId,
            saleUid: 0
          })),
    })
  },
  changeLiveStatus: function(id, status){
    wx.request({
      url: app.globalData.host + '/api/weapp/RefrushLiveStatus?id='+ id +'&liveStatus=' + status,
      success: function(res){
      }
    })
  },
  editor: function(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../apply/index?id=' + id,
    })
  },
  delete: function(e){
    let that = this;
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '是否确认删除？',
      success: function(res){
        if(res.confirm){
          wx.request({
            url: app.globalData.host + '/api/WeApp/DeleteLive?id=' + id,
            success: function(res){
              wx.showToast({
                title: '删除成功',
              })
              that.getLiveList();
            },
          })
        }
      }
    })
  },
  showRuler: function() {
    this.setData({
      showRuler: !this.data.showRuler
    })
  },

    //填写判断
    checkWrite: function(str) {
      wx.showModal({
        title: '提示',
        content: str,
        showCancel: false
      })
    },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})